﻿(function($){    
    //弹出窗口层插件
	$.openWindow={
         defaults:{
		         window:"self",
			     type:0,
				 size:[420,240],
				 title:"温馨提示",
				 content:"信息内容",
				 skin:"openWindow/default",
				 showShadow:true,
				 shadowStyle:{opacity:"0.6",bgColor:"#000"},
				 dragAble:true,
				 position:["auto","auto"]
		 },
		 option:null,
	     setOption:function(option){
		    //设置参数			
			if(option){
			  this.option=option;
			  $.extend(this.defaults,option);
			}
		 },
		 layer:null,
		 shadow:null,
		 clickButton:"",
		 init:function(option){
		      this.setOption(option);
			  var defaults=this.defaults;
		      var contentHtml=[];
			  var myWindow=(defaults.window=="self")?window:window.top;
			  var myDocument=this.layer=$(myWindow.document.body);
			  var myWindowHead=myDocument.prevAll("head");
			  //添加样式
			  if(!myWindowHead.find("link[title='openWindow']").length>0)myWindowHead.append("<link type=\"text/css\" title=\"openWindow\" rel=\"stylesheet\" href=\""+defaults.skin+"/style.css\" />");
			  //添加弹出层
			  myDocument.prepend("<div class=\"open_window\"></div>");
			  var openWindow=myDocument.find("div.open_window").filter(":first");
			  //添加遮罩层
			  if(defaults.showShadow||this.option.shadowStyle){
			     defaults.showShadow=true;
			     if(!myDocument.find(".open_window_shadow").length>0)myDocument.prepend("<div class=\"open_window_shadow\"></div>");
			     var windowShadow=this.shadow=myDocument.find("div.open_window_shadow");
			  };		  
			  //添加标题栏
			  $("<div class=\"win_titbar\">"+defaults.title+"<span href=\"\" class=\"close_win\"><img src=\""+defaults.skin+"/ico_close.gif\" alt=\"关闭窗口\" title=\"关闭窗口\" /></span></div>").appendTo(openWindow);
			  var titleBar=openWindow.children("div.win_titbar"); 
			  //内容部分配置
			  switch(defaults.type){
			       case 0://如果是alert窗口
				   defaults.content="<p class=\"alert_box\">"+defaults.content+"</p><p class=\"done\"><input type=\"button\" class=\"btn_sure\" value=\"\" /></p>";
				   break;
				   case 1://如果是confirm窗口
				   defaults.content="<p class=\"confirm_box\">"+defaults.content+"</p><p class=\"done\"><input type=\"button\" class=\"btn_sure\" value=\"\" /><input type=\"button\" class=\"btn_cancle\" value=\"\" /></p>";
				   break;
				   case 2://如果是showModalDialog
				   defaults.content="<iframe src=\""+defaults.content+"\" frameBorder=\"0\" style=\"height:"+parseInt(openWindow.height()-titleBar.outerHeight())+";width:"+parseInt(openWindow.width()-titleBar.outerWidth())+"\"></iframe>";
				   break;
				   case 3://如果是普通的弹出层openLayer
				   break;
			  }	
			  //添加内容部分
			  contentHtml.push("<div class=\"win_content\">");
			  contentHtml.push(defaults.content);
			  contentHtml.push("</div>");
			  $(contentHtml.join("")).appendTo(openWindow);
			  var openWindowCon=openWindow.children("div.win_content");
			  //提取确定取消按钮
			  var btnSure=defaults.type==2?openWindowCon.children("iframe").find(".btn_sure"):openWindowCon.find(".btn_sure");
			  var btnCancle=defaults.type==2?openWindowCon.children("iframe").find(".btn_cancle"):openWindowCon.find(".btn_cancle");
			  //初始化弹出窗口层状态			  
			  initWindow();
			  $(window).resize(function(){
			        initWindow();
			  });
			  $(window).scroll(function(){
			        initWindow();
			  });
			  //层定位	
			  function initWindow(){
                 var body=myWindow.document.documentElement;			  
			     var win=$(myWindow);
				 var bodyHeight=body.clientHeight;
			     var bodyWidth=body.clientWidth;
				 var winScrollTop=win.scrollTop();
				 var winScrollLeft=win.scrollLeft();
			     if(windowShadow){				     
				     windowShadow.css({position:"absolute",left:winScrollLeft,top:winScrollTop,width:bodyWidth,height:bodyHeight,background:defaults.shadowStyle.bgColor,opacity:defaults.shadowStyle.opacity,zIndex:"999"});
                     if($.browser.mozilla){
					   windowShadow.css("width",bodyWidth+17);
					 }					 
			     }
                 openWindow.css({position:"absolute",left:(bodyWidth-defaults.size[0])/2+winScrollLeft,top:(bodyHeight-defaults.size[1])/2+winScrollTop,width:defaults.size[0]+"px",height:defaults.size[1]+"px",zIndex:"999999",overflow:"hidden"});
				 if(!isNaN(defaults.position[0])){
				    openWindow.css({"left":defaults.position[0]+winScrollLeft}); 
				 }else if(!isNaN(defaults.position[1])){
				    openWindow.css({"top":defaults.position[1]+winScrollTop}); 
				 } 				 
			  };
              //关闭按钮
			  titleBar.find("span.close_win").bind("click",function(){
			        $.openWindow.closeWindow();
			  });
              //如果弹出层允许拖动
			  if(defaults.dragAble){
			      var isMovingLayer=false;
				  var posX,posY;
				  titleBar.mousedown(function(event){
					   isMovingLayer=true;
					   $(this).css("cursor","move");
					   posX=event.pageX-parseInt(openWindow.css("left"));
					   posY=event.pageY-parseInt(openWindow.css("top"));
                       openWindow.fadeTo("fast",0.5);					   
				  }).mouseup(function(){
				       isMovingLayer=false;
					   $(this).css("cursor","pointer");
					   openWindow.fadeTo("fast",1);
				  });
				  myDocument.mousemove(function(event){
					  if(isMovingLayer){
					     var nowPosX=event.pageX-posX;
						 var nowPosY=event.pageY-posY;					 
						 openWindow.css({top:nowPosY,left:nowPosX});						 
					  }
				  });
			  };
			  //关闭窗口
			  function closeWindow(){
			        openWindow.empty().remove();
					myWindowHead.find("link[title='openWindow']").remove();
					if(windowShadow){windowShadow.remove();};
			  };
			  //
			  if(btnSure.length>0){
			    btnSure.click(function(){
			        $.openWindow.closeWindow();
			        $.openWindow.clicked("sure");
				});
			  }
		 },
		 closeWindow:function(){
		      this.layer.find("div.open_window").filter(":first").empty().remove();
              this.layer.children("head").find("link[title='openWindow']").remove();
		      if(this.shadow){this.shadow.remove();};           
		 },
		 alert:function(t){
		      var arg=arguments;
			  var title,text;
			  if(arg.length<2){
			      text=arg.length==0?"弹出信息":t;
		          this.init({type:0,content:text});
			  }else if(arg.length==2){
			      title=arg[0],text=arg[1];
				  this.init({type:0,title:title,content:text});
			  }
		 },
		 confirm:function(t){
		      var arg=arguments;
			  var title,text;
			  if(arg.length<2){
			      text=arg.length==0?"弹出信息":t;
		          this.init({type:1,content:text});
			  }else if(arg.length==2){
			      title=arg[0],text=arg[1];
				  this.init({type:1,title:title,content:text});
			  }
		 },
		 showModalDialog:function(url){
		      var arg=arguments;
			  var title,url;
			  if(arg.length<2){
			      url=arg.length==0?"弹出信息":t;
		          this.init({type:1,content:url});
			  }else if(arg.length==2){
			      title=arg[0],url=arg[1];
				  this.init({type:1,title:title,content:url});
			  }
		 },
		 clicked:function(name){
		      return name; 
		 }
	};   
})(jQuery);